package com.itheima.service.impl;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.pojo.*;
import com.itheima.utils.IdWorker;
import com.itheima.dao.AdminMapper;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysDictDataServiceImpl implements SysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    @Transactional
    public void add(SysDictDataVO sysDicTDataVO) {
        SysDictData sysDictData = JSON.parseObject(JSON.toJSONString(sysDicTDataVO), SysDictData.class);
        IdWorker idWorker = new IdWorker();
        long l = idWorker.nextId();
        String uid = String.valueOf(l);

        Admin admin = adminMapper.findAdminByUsername("admin");
        String adminId = admin.getUid();
        sysDictData.setCreateByUid(adminId);
        sysDictData.setUpdateByUid(adminId);
        sysDictData.setUid(uid);
        sysDictDataMapper.insert(sysDictData);
    }

    @Override
    @Transactional
    public void deleteBatch(List<SysDictDataVO> sysDictDataVOList) {
        for (SysDictDataVO sysDictDataVO : sysDictDataVOList) {
            String uid = sysDictDataVO.getUid();
            sysDictDataMapper.deleteByPrimaryKey(uid);
        }
    }

    @Override
    @Transactional
    public void edit(SysDictDataVO sysDictDataVO) {
        SysDictData sysDictData = JSON.parseObject(JSON.toJSONString(sysDictDataVO), SysDictData.class);
        String uid = sysDictData.getUid();
        Example example = new Example(SysDictData.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid",uid);
        sysDictData.setUpdateTime(new Date());
        Admin admin = adminMapper.findAdminByUsername("admin");
        String adminId = admin.getUid();
        sysDictData.setCreateByUid(adminId);
        sysDictData.setUpdateByUid(adminId);
        sysDictDataMapper.updateByExample(sysDictData,example);
    }

    @Override
    public PageInfo<SysDictData> getList(SysDictDataVO sysDictDataVO) {

        PageHelper.startPage( sysDictDataVO.getCurrentPage(),sysDictDataVO.getPageSize());
        Example example = createExample(sysDictDataVO);
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectByExample(example);
        for (SysDictData sysDictData : sysDictDataList) {
            //封装字典类型数据
            String dictTypeUid = sysDictData.getDictTypeUid();
            SysDictType sysDictType = new SysDictType();
            sysDictType.setUid(dictTypeUid);
            SysDictType sysDictType1 = sysDictTypeMapper.selectOne(sysDictType);
            sysDictData.setSysDictType(sysDictType1);
        }
        return new PageInfo<>(sysDictDataList);
    }

    @Override
    public List<Map> getListByDictType(String dictType) {
        //根据字典类型查到对应的uid
        Example example = new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("dictType",dictType);
        SysDictType sysDictType = sysDictTypeMapper.selectOneByExample(example);
        String dictTypeUid = sysDictType.getUid();

        //根据字典类型的uid查询到字典数据列表
        List<SysDictData> sysDictDataList = sysDictDataMapper.findByDictTypeUid(dictTypeUid);

        //将结果转换为map封装起来
        List<Map> mapList = new ArrayList<>();
        HashMap<String,Object> hashMap = new HashMap<>();
        for (SysDictData sysDictData : sysDictDataList) {
            hashMap.put("oid",sysDictData.getOid());
            hashMap.put("dictLabel",sysDictData.getDictLabel());
            hashMap.put("dictValue",sysDictData.getDictValue());
            hashMap.put("sort",sysDictData.getSort());
            hashMap.put("dictTypeUid",sysDictData.getDictTypeUid());
            hashMap.put("listClass",sysDictData.getListClass());
            hashMap.put("isDefault",sysDictData.getIsDefault());
            hashMap.put("isPublish",sysDictData.getIsPublish());
            hashMap.put("createByUid",sysDictData.getCreateByUid());
            hashMap.put("updateByUid",sysDictData.getUpdateByUid());
            hashMap.put("remark",sysDictData.getRemark());
            hashMap.put("uid",sysDictData.getUid());
            hashMap.put("status",sysDictData.getStatus());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            hashMap.put("createTime",simpleDateFormat.format(sysDictData.getCreateTime()));
            hashMap.put("updateTime",simpleDateFormat.format(sysDictData.getUpdateTime()));
            mapList.add(hashMap);
        }
        return mapList;
    }

    /**
     * 根据字典类型数组获取字典数据
     * @param dictTypeList
     * @return
     */
    @Override
    public List<Map> getListByDictTypeList(String[] dictTypeList) {
        List<Map> mapList = new ArrayList<>();
        for (String s : dictTypeList) {
            List<Map> listByDictType = this.getListByDictType(s);
            for (Map map : listByDictType) {
                mapList.add(map);
            }
        }
        return mapList;
    }

    /*
     * 封装查询条件对象*/
    private Example createExample(SysDictDataVO sysDictDataVO) {

        Example example = new Example(SysDictData.class);
        example.setOrderByClause("sort DESC");
        Example.Criteria criteria = example.createCriteria();
        if (sysDictDataVO!= null) {
            // 根据字典分类uid精准查询
            if(sysDictDataVO.getDictTypeUid()!=null && !"".equals(sysDictDataVO.getDictTypeUid())){
                criteria.andEqualTo("dictTypeUid",sysDictDataVO.getDictTypeUid());
            }
            //根据字典标签模糊查询
            if(sysDictDataVO.getDictLabel()!=null && !"".equals(sysDictDataVO.getDictLabel())){
                criteria.andLike("dictLabel",sysDictDataVO.getDictLabel());
            }
        }
        return example;
    }

}
